﻿using IService.Blog;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Threading.Tasks;

namespace BlogAPI.Controllers
{
    /// <summary>
    /// 站点管理
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize]
    //[AllowAnonymous]
    public class WebSiteController : ControllerBase
    {
        readonly ILogger<WebSiteController> logger;
        readonly IWebSiteService webSiteService;
        public WebSiteController(ILogger<WebSiteController> _logger, IWebSiteService _webSiteService)
        {
            logger = _logger;
            webSiteService = _webSiteService;
        }

        /// <summary>
        /// 查询所有站点（分页）
        /// </summary>
        /// <param name="offset">页数</param>
        /// <param name="limit">每页数</param>
        /// <returns></returns>
        [HttpPost]
        [Description("查询所有站点")]
        public async Task<MessageModel<PageInfoModel<BWebSite>>> GetAllToPage(int offset=1, int limit=15)
        {
            try
            {
                return await webSiteService.GetAllToPage(offset, limit);
            }
            catch (Exception ex)
            {
                logger.LogError(ex.Message);
                return new MessageModel<PageInfoModel<BWebSite>>()
                {
                    Message = "服务异常" + ex.Message,
                };
            }
        }


        /// <summary>
        /// 查询所有站点
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Description("查询所有站点")]
        public async Task<MessageModel<List<BWebSite>>> GetAll()
        {
            try
            {
                return await webSiteService.GetAll();
            }
            catch (Exception ex)
            {
                logger.LogError(ex.Message);
                return new MessageModel<List<BWebSite>>()
                {
                    Message = "服务异常" + ex.Message,
                };
            }

        }

        /// <summary>
        /// 按ID查询站点
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Description("按ID查询站点")]
        public async Task<MessageModel<BWebSite>> GetById(int id)
        {
            try
            {
                return await webSiteService.GetById(id);
            }
            catch (Exception ex)
            {
                logger.LogError(ex.Message);
                return new MessageModel<BWebSite>()
                {
                    Message = "服务异常" + ex.Message,
                };
            }

        }


        /// <summary>
        /// 添加站点
        /// </summary>
        /// <param name="parameter"></param>
        /// <returns></returns>
        [HttpPost]
        [Description("添加站点")]
        public async Task<MessageModel<string>> Post([FromBody] BWebSite parameter)
        {
            try
            {
                return await webSiteService.Post(parameter);
            }
            catch (Exception ex)
            {
                logger.LogError(ex.Message);
                return new MessageModel<string>()
                {
                    Message = "服务异常" + ex.Message,
                };
            }

        }

        /// <summary>
        /// 修改站点
        /// </summary>
        /// <param name="parameter"></param>
        /// <returns></returns>
        [HttpPut]
        [Description("修改站点")]
        public async Task<MessageModel<string>> Put([FromBody] BWebSite parameter)
        {
            try
            {
                return await webSiteService.Put(parameter);
            }
            catch (Exception ex)
            {
                logger.LogError(ex.Message);
                return new MessageModel<string>()
                {
                    Message = "服务异常" + ex.Message,
                };
            }

        }

        /// <summary>
        /// 删除站点
        /// </summary>
        /// <returns></returns>
        [HttpDelete]
        [Description("删除站点")]
        public async Task<MessageModel<bool>> DelById(int id)
        {
            try
            {
                return await webSiteService.DelById(id);
            }
            catch (Exception ex)
            {
                logger.LogError(ex.Message);
                return new MessageModel<bool>()
                {
                    Message = "服务异常" + ex.Message,
                };
            }

        }


    }
}
